package com.eudon.design_pattern.behavioral.memento;

import java.util.Stack;

/**
 * @author eudon
 * @description 输入的文本
 * @date 2025/9/3
 * @email syd19970616@gmail.com
 * @address <a href="https://gitee.com/eudon">Gitee</a>
 */
public class InputText {
    private final StringBuilder textBuilder = new StringBuilder();
    private final Stack<TextSnapShot> snapShotStack = new Stack<>();

    public void append(String text) {
        TextSnapShot snapShot = new TextSnapShot(textBuilder.toString());
        snapShotStack.push(snapShot);
        textBuilder.append(text);
    }

    public String restore() {
        if (!snapShotStack.isEmpty()) {
            TextSnapShot poll = snapShotStack.pop();
            textBuilder.replace(0, textBuilder.length(), poll.getText());
        } else {
            textBuilder.replace(0, textBuilder.length(), "");
        }
        return textBuilder.toString();
    }

    public String text() {
        return textBuilder.toString();
    }
}
